<?php
namespace app\controller\admin;

use app\BaseController;
use app\Request;
use think\facade\Db;
use think\Collection;

class Box extends BaseController{
    public function index(Request $request)
    {
        $param = $request->param();
        $member = Db::name("scale_prize_box");
        if(isset($param['prize_box_name']) && !empty($param['prize_box_name'])){
            $member = $member->whereLike('prize_box_name', '%'.$param['prize_box_name'].'%');
        }
        if(isset($param['status']) && $param['status'] !=''){
            $member = $member->where('status', $param['status']);
        }
        $member = $member->where('is_del', 0)
            ->paginate($param['limit']);

        return api_json(20000,'请求数据成功',$member);
    }

    public function list(Request $request)
    {
        $scale_prize_box = Db::name("scale_prize_box")
            ->where('status',1)
            ->where('is_del',0)
            ->column('id,prize_box_name','id');
        return api_json(20000,'请求数据成功',$scale_prize_box);
    }

    public function info(Request $request)
    {
        $param = $request->param();
        $member = Db::name("scale_prize_box")
            ->where('id',$param['id'])
            ->find();
        return api_json(20000,'请求数据成功',$member);
    }

    public function changeStatus(Request $request)
    {
        $param = $request->param();
        $res = Db::name('scale_prize_box')
            ->where('id',$param['id'])
            ->data(['status'=>$param['status']])
            ->update();
        if($res){
            return api_json(20000,'操作成功',[]);
        }else{
            return api_json(0,'操作失败',[]);
        }
    }

    public function delBox(Request $request)
    {
        $param = $request->param();
        $res = Db::name('scale_prize_box')
            ->where('id',$param['id'])
            ->data(['is_del'=>1])
            ->update();
        if($res){
            return api_json(20000,'操作成功',[]);
        }else{
            return api_json(0,'操作失败',[]);
        }
    }
    public function create(Request $request)
    {
        $param = $request->param();
        $data = [
          "prize_box_name" => $param['prize_box_name'],
          "price" => $param['price'],
          "status" => $param['status'],
          "is_del" => 0
        ];

        $res = Db::name('scale_prize_box')
            ->save($data);
        if($res){
            return api_json(20000,'保存成功',[]);
        }else{
            return api_json(0,'保存失败',[]);
        }
    }
    public function update(Request $request)
    {
        $param = $request->param();
        $data = [
          "id" => $param['id'],
          "prize_box_name" => $param['prize_box_name'],
          "price" => $param['price'],
          "status" => $param['status']
        ];

        $res = Db::name('scale_prize_box')
            ->save($data);
        if($res){
            return api_json(20000,'保存成功',[]);
        }else{
            return api_json(0,'保存失败',[]);
        }
    }
}